//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using OpenLS.Chart.UI;

namespace OpenLS.Spreadsheet.UI.Internals
{
    class ChartTemplateToImageSourceConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;
            var u = new Uri(GetName(value));
            StreamResourceInfo info = Application.GetResourceStream(u);
            if (info == null)
                return null;
            var bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.StreamSource = info.Stream;
            bitmap.EndInit();
            return bitmap;
        }
        const string Prefix = "pack://application:,,/OpenLS.Media;component/";

        private static string GetName(object value)
        {
            if (value is Column3DChartTemplate)
            {
                var t = (Column3DChartTemplate)value;
                return
                    string.Format(Prefix + "Column3D{0}{1}.png", t.Grouping, t.Shape);
            }
            if (value is ColumnChartTemplate)
            {
                var t = (ColumnChartTemplate)value;
                return
                    string.Format(Prefix + "Column{0}.png", t.Grouping);
            }
            if (value is LineChartTemplate)
            {
                var t = (LineChartTemplate)value;
                string marker = t.Marker ? "Marker" : "";
                return
                    string.Format(Prefix + "Line{0}{1}.png", t.Grouping, marker);
            }
            if (value is Line3DChartTemplate)
            {
                return string.Format(Prefix + "Line3D.png"); 
            }
            if (value is Bar3DChartTemplate)
            {
                var t = (Bar3DChartTemplate) value;
                return string.Format(Prefix + "Bar3D{0}.png", t.Grouping); 
            }
            if (value is BarChartTemplate)
            {
                var t = (BarChartTemplate) value;
                return
                    string.Format(Prefix + "Bar{0}.png", t.Grouping);
            }
            if (value is Area3DChartTemplate)
            {
                var t = (Area3DChartTemplate) value;
                return string.Format(Prefix + "Area3D{0}.png", t.Grouping);
            }
            if (value is AreaChartTemplate)
            {
                var t = (AreaChartTemplate)value;
                return
                    string.Format(Prefix + "Area{0}.png", t.Grouping);
            }
            if (value is PieChartTemplate)
            {
                var t = (PieChartTemplate)value;
                return
                    string.Format(Prefix + "Pie{0}.png", t.IsExploded ? "Exploded" : null);
            }
            if (value is Pie3DChartTemplate)
            {
                var t = (Pie3DChartTemplate)value;
                return
                    string.Format(Prefix + "Pie3D{0}.png", t.IsExploded ? "Exploded" : null);
            }
            if (value is ScatterChartTemplate)
            {
                var t = (ScatterChartTemplate) value;
                string s = "";
                if (!t.NoLine)
                    s = t.IsSmooth ? "Smooth" : "Line";

                return
                    string.Format(Prefix + "Scatter{0}{1}.png", s, t.HasMarkers? "Markers" : null);
            }
            throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        #endregion
    }
}
